//package com.dream.service.recommender.hub.rec;
//
//import com.alibaba.fastjson.JSONObject;
//import com.dream.common.model.response.BaseResponse;
//import com.dream.common.model.response.ErrorCode;
//import com.dream.service.product.domain.entity.SpuBase;
//import com.dream.service.recommender.hub.util.ParamUtil;
//import com.dream.service.recommender.toolkits.cache.RedisCache;
//import com.dream.service.recommender.toolkits.utils.ConfigUtil;
//import com.dream.service.recommender.toolkits.utils.StringUtils;
//import com.dream.service.search.api.SearchRecallService;
//import com.dream.service.search.domain.entity.ProductRecallResult;
//import com.dream.service.search.domain.request.ProductSearchRequest;
//import com.dream.service.wrapper.domain.entity.SPUData;
//import com.dream.service.recommender.base.compare.WeightComparator;
//import com.dream.service.recommender.base.impl.BaseRecommenderService;
//import com.dream.service.recommender.domain.request.BaseRequest;
//import com.dream.service.recommender.domain.request.FrontParam;
//import com.dream.service.recommender.domain.response.RecommenderData;
//import com.dream.service.recommender.domain.response.RecommenderItem;
//import com.dream.service.recommender.domain.response.RecommenderSku;
//import com.em.service.character.api.UserPvService;
//import com.em.service.character.domain.model.PvSecquenceBean;
//import com.em.service.character.domain.model.UserPvSequenceModelBean;
//import org.apache.log4j.Logger;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import java.util.*;
//
///**
// * 根据分类召回
// * Created by duxy on 2016/04/25.
// */
//public class FootStepRecommender extends BaseRecommenderService {
//
//    private Logger logger = Logger.getLogger(FootStepRecommender.class);
//
//    private SearchRecallService searchRecallService;
//    private UserPvService userPvService;
//
//
//    public SearchRecallService getSearchRecallService() {
//        return searchRecallService;
//    }
//
//
//    @Autowired
//    public void setSearchRecallService(SearchRecallService searchRecallService) {
//        this.searchRecallService = searchRecallService;
//    }
//
//
//    public UserPvService getUserPvService() {
//        return userPvService;
//    }
//
//    @Autowired
//    public void setUserPvService(UserPvService userPvService) {
//        this.userPvService = userPvService;
//    }
//
//
//    // 获取商品数据倍数
//    private int multiple = 5;
//
//    // 失效时间间隔 (sec)
//    private int residue_time_interval = Integer.parseInt(ConfigUtil.getPro("residue_time_interval_ca"));
//
//    // 缓存
//    private final String cacheKey = "rec_category_list_";
//
//    // 缓存
//    private RedisCache redisCache = RedisCache.getInstance();
//
//    @Override
//    public BaseResponse<RecommenderData> getRecommender(BaseRequest<FrontParam> frontParamBaseRequest) {
//        long sTime = System.currentTimeMillis();
//        BaseResponse<RecommenderData> recommenderResponse = new BaseResponse<RecommenderData>();
//        try {
//            if (frontParamBaseRequest == null || frontParamBaseRequest.getParam() == null || frontParamBaseRequest.getParam().getUser() == null) {
//                return buildResult(recommenderResponse, false, ErrorCode.PARAM_ERROR.getCode(), ErrorCode.PARAM_ERROR.getError_msg(), null);
//            }
//            FrontParam frontParam = frontParamBaseRequest.getParam();
//            String uuid = ParamUtil.getUUid(frontParam);
//            String pin = ParamUtil.getPin(frontParam);
//            if (pin == null && uuid == null) return null;
////            String concat_major_id = "user_sq_pv_";
////            concat_major_id += pin != null ? ("pin_" + pin) : ("uuid_" + uuid);
//
//            System.out.println(pin + "," + uuid);
//            BaseResponse<UserPvSequenceModelBean> response = userPvService.getUserSequencePv(pin, uuid);
//            System.out.println("response:" + JSONObject.toJSONString(response));
//            List<PvSecquenceBean> pvSecquenceBeanList = response.getResult().getPvSecquence();
//
//            if (response == null || response.getResult() == null || pvSecquenceBeanList == null) {
//                recommenderResponse.setSuccess(true);
//                recommenderResponse.setLatency(System.currentTimeMillis() - sTime);
//                recommenderResponse.setError_msg("no record");
//                recommenderResponse.setError_code(0);
//                return recommenderResponse;
//            }
//
//
//            HashMap<Long, String> pvSpuTimestamp = new HashMap<Long, String>();
//
//            Set<Long> needspus = new HashSet<Long>();
//            for (PvSecquenceBean pvSecquenceBean : pvSecquenceBeanList) {
//                needspus.add(pvSecquenceBean.getSpu());
//                pvSpuTimestamp.put(pvSecquenceBean.getSpu(), pvSecquenceBean.getTimestamp());
////                System.out.println(pvSecquenceBean.getSpu());
//            }
//
//
//            List<ProductRecallResult> recallResults = new ArrayList<ProductRecallResult>();
////            SPUDataRequest sPUDataRequest = new SPUDataRequest();
////            sPUDataRequest.setSpus(new HashSet<Long>(needspus));
//
////            List<ProductRecallResult> recallResults;
//
//            RecommenderData recommenderData = buildResult(needspus, pvSpuTimestamp);
//            System.out.println("recommenderData+++" + JSONObject.toJSONString(recommenderData));
//            recommenderResponse = buildResult(recommenderResponse, true, ErrorCode.OK.getCode(), "", recommenderData);
//
//            System.out.println("res::::" + JSONObject.toJSONString(recommenderResponse.getResult()));
//            Set<Long> spus = rebuildPositionInfo(recommenderResponse.getResult());
//            long usedTime = System.currentTimeMillis() - sTime;
//            recommenderData.setLatency(usedTime);
//            recommenderResponse.setLatency(usedTime);
//            return recommenderResponse;
//        } catch (Exception e) {
//            logger.error("FootStepRecommender===>getRecommender:::", e);
//            return buildResult(recommenderResponse, false, ErrorCode.PROCESS_ERROR.getCode(), e.getMessage(), null);
//        }
//    }
//
//
//    /**
//     * 更新位置信息
//     *
//     * @param recommenderData
//     * @return
//     */
//    public Set<Long> rebuildPositionInfo(RecommenderData recommenderData) {
//        if (recommenderData == null || StringUtils.isEmpty(recommenderData.getList())) {
//            return null;
//        }
//        List<RecommenderItem> items = recommenderData.getList();
//        Set<Long> spus = new HashSet<Long>();
//        int pos = 1;
//        for (RecommenderItem recommenderItem : items) {
//            recommenderItem.setPos(pos++);
//            if (recommenderItem instanceof RecommenderSku) {
//                try {
//                    spus.add(((RecommenderSku) recommenderItem).getSpu());
//                } catch (Exception e) {
//                    logger.error("FootStepRecommender===>rebuildPositionInfo:::", e);
//                }
//            }
//        }
//        return spus;
//    }
//
//
//    private List<ProductRecallResult> gatherSearchRecallData(List<Long> needKeys) {
//
//        ProductSearchRequest request = new ProductSearchRequest();
//
//
//        request.setNeedKeys(needKeys);
//
//
//        try {
//            if (searchRecallService == null) {
//                return null;
//            }
//            BaseResponse<List<ProductRecallResult>> recallResponse = searchRecallService.productSearchRecall(request);
////            System.out.println(JSONObject.toJSONString(recallResponse));
//            if (recallResponse.isSuccess()) {
//                return recallResponse.getResult();
//            }
//        } catch (Exception e) {
//            logger.error("CategoryListRecommender===>gatherSearchRecallData:::", e);
//        }
//        return null;
//    }
//
//
//    /**
//     * 封装结果
//     *
//     * @param needspus
//     * @return
//     */
//    private RecommenderData buildResult(Set<Long> needspus, HashMap<Long, String> pvSpuTimestamp) {
//
//
//        Map<Long, SPUData> spuDataMap = getBaseInfo(needspus);
//        System.out.println("*************" + spuDataMap.size());
////            BaseResponse<Map<Long, SPUData>> spuData = productWrapperService.getSPUData(sPUDataRequest);
//
//
//        Iterator spuDataEntry = spuDataMap.entrySet().iterator();
//        RecommenderData<RecommenderSku> recommenderData = new RecommenderData<RecommenderSku>();
//        List<RecommenderSku> recommenderSkus = new ArrayList<RecommenderSku>();
//        recommenderData.setList(recommenderSkus);
//
//        if (spuDataMap == null || spuDataMap.isEmpty()) {
//            return recommenderData;
//        }
//
//        while (spuDataEntry.hasNext()) {
//            Map.Entry _spudata = (Map.Entry) spuDataEntry.next();
//            System.out.println(_spudata.getKey());
//            try {
//                SPUData spuData = (SPUData) _spudata.getValue();
//                RecommenderSku recommenderSku = buildProduct(spuDataMap.get(_spudata.getKey()));
//                System.out.println("JSONObject.toJSONString(recommenderSku)" + JSONObject.toJSONString(recommenderSku));
//                if (recommenderSku != null) {
//                    recommenderSkus.add(recommenderSku);
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//
//        SortAndRankByTimestamp(recommenderData, pvSpuTimestamp);
//        return recommenderData;
//
//    }
//
//
//    /**
//     * 封装商品信息
//     *
//     * @param spuData
//     * @return
//     */
//    private RecommenderSku buildProduct(SPUData spuData) {
//        if (spuData == null || spuData.getSpuBase() == null/* || spuData.getStockStatus() == StockStatus.NO || spuData.getSpuBase().getIsPutOn() == 0*/) {
//            return null;
//        }
//        RecommenderSku recommenderSku = new RecommenderSku();
//        SpuBase spuBase = spuData.getSpuBase();
//        recommenderSku.setSpu(spuBase.getSpu());
//        recommenderSku.setSale_type(spuBase.getSale_type());
//        recommenderSku.setMain_sku(spuBase.getMain_sku());
//        recommenderSku.setMain_title(spuBase.getMain_title());
////        recommenderSku.setW(recallResult.getQuality_score());
//
//        if (!StringUtils.isEmpty(spuBase.getMain_image_url())) {
//            recommenderSku.setMain_image_url(spuBase.getMain_image_url());
//        } else {
//            return null;
//        }
//        if (spuData.getSkuPrice() != null) {
//            recommenderSku.setSkuPrice(spuData.getSkuPrice());
//        } else {
//            return null;
//        }
//        return recommenderSku;
//    }
//
//    /**
//     * 穿插排序 (通过类目穿插)
//     *
//     * @param recommenderData
//     * @param pvSpuTimestamp
//     * @return
//     */
//    public RecommenderData SortAndRankByTimestamp(RecommenderData recommenderData, HashMap<Long, String> pvSpuTimestamp) {
//        if (recommenderData == null) {
//            return recommenderData;
//        }
//
//        List<RecommenderSku> sortRecommenderItems = new ArrayList<RecommenderSku>(recommenderData.getList());
//
//        for (RecommenderSku recommenderSku : sortRecommenderItems) {
//
//            recommenderSku.setW(Double.parseDouble(pvSpuTimestamp.get(recommenderSku.getSpu())));
//        }
//        Collections.sort(sortRecommenderItems, new WeightComparator());
//        System.out.println("^^^^^^^^^^^^" + sortRecommenderItems);
//        recommenderData.setList(sortRecommenderItems);
//
//        return recommenderData;
//    }
//
//}
